<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>

description("This tests the constructor for the SecurityPolicyViolationEvent DOM class.");

var testObject = {nyannyan: 123};
var testDiv = document.createElement("div");

// No initializer is passed.
shouldBeTrue("new SecurityPolicyViolationEvent('eventType').bubbles");
shouldBeFalse("new SecurityPolicyViolationEvent('eventType').cancelable");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').documentURI");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').referrer");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').blockedURI");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').violatedDirective");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').effectiveDirective");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').originalPolicy");
shouldBeEmptyString("new SecurityPolicyViolationEvent('eventType').sourceFile");
shouldBeEqualToString("new SecurityPolicyViolationEvent('eventType').disposition", "enforce");
shouldBe("new SecurityPolicyViolationEvent('eventType').lineNumber", "0");
shouldBe("new SecurityPolicyViolationEvent('eventType').columnNumber", "0");
shouldBe("new SecurityPolicyViolationEvent('eventType').statusCode", "0");

/*
required  DOMString documentURI;
required  DOMString violatedDirective;
required  DOMString effectiveDirective;
required  DOMString originalPolicy;
required  SecurityPolicyViolationEventDisposition disposition;
required  unsigned short statusCode;
*/
// This function will add to the init dict any missing required members
function add_required_members(init_dict, deleted_member) {
  ["documentURI", "violatedDirective", "effectiveDirective", "originalPolicy"].forEach(function(member) {
    if (!(member in init_dict)) init_dict[member] = 'bar';
  })
  if (!("statusCode" in init_dict)) init_dict["statusCode"] = 200;
  if (!("disposition" in init_dict)) init_dict["disposition"] = 'enforce';

  // for tests that test the absence of required members
  if (deleted_member != undefined && deleted_member in init_dict) delete init_dict[deleted_member];

  return init_dict;
}

// bubbles is always true.
shouldBeTrue("new SecurityPolicyViolationEvent('eventType', add_required_members({ bubbles: false})).bubbles");
shouldBeTrue("new SecurityPolicyViolationEvent('eventType', add_required_members({ bubbles: true})).bubbles");

// cancelable is always false.
shouldBeFalse("new SecurityPolicyViolationEvent('eventType', add_required_members({ cancelable: false})).cancelable");
shouldBeFalse("new SecurityPolicyViolationEvent('eventType', add_required_members({ cancelable: true})).cancelable");

// String members are passed.
["documentURI", "referrer", "blockedURI", "violatedDirective", "effectiveDirective", "originalPolicy", "sourceFile"].forEach(function(member) {
    shouldBeEqualToString("new SecurityPolicyViolationEvent('eventType', add_required_members({ " + member + ": 'foo' }))." + member, "foo");
});

// Enum members throw on unknown initializer values.
shouldThrow("new SecurityPolicyViolationEvent('eventType', add_required_members({ disposition: 'foo' }))");

// Number members are passed.
["lineNumber", "columnNumber", "statusCode"].forEach(function(member) {
    shouldBe("new SecurityPolicyViolationEvent('eventType', add_required_members({ " + member + ": 42 }))." + member, "42");
});

// Missing required members throw
["documentURI", "violatedDirective", "effectiveDirective", "originalPolicy", "statusCode", "disposition"].forEach(function(member) {
   shouldThrow("new SecurityPolicyViolationEvent('eventType', add_required_members({}, \"" + member + "\"))")
});
</script>
</body>
</html>
